package com.xhwl.logistics.strategy.impl;

import com.xhwl.common.dto.cent.device.DeviceDTO;
import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.common.query.cent.device.DeviceQuery;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleSystemInfoBO;
import com.xhwl.logistics.dto.http.ParkingLotDeviceInfoKeTopDTO;
import com.xhwl.logistics.dto.http.ParkingLotDeviceKeTopDTO;
import com.xhwl.logistics.dto.http.query.QueryParkingLotDeviceKeTopDTO;
import com.xhwl.logistics.service.cent.IDeviceService;
import com.xhwl.logistics.service.impl.http.KeTopHttpServiceImpl;
import com.xhwl.logistics.service.impl.kafka.VehicleAlarmKafkaServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotDeviceHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author msy
 */
@Slf4j
//@Service
public class VehicleParkingLotKeyTopDeviceHandlerImpl implements IVehicleParkingLotDeviceHandler {

    @Autowired
    private KeTopHttpServiceImpl keTopHttpService;

    @Autowired
    private IDeviceService deviceService;

    @Autowired
    private VehicleAlarmKafkaServiceImpl vehicleAlarmKafkaService;


    @Override
    public void syncDeviceWithStatus(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        log.info("KeyTop sync device status start!");
        log.info("vehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);

        VehicleSystemInfoBO vehicleSystemInfoBO = vehicleIntegratedConfigBO.getVehicleSystemInfoBO();
        Integer projectId = vehicleSystemInfoBO.getProjectId();

        ParkingLotDeviceKeTopDTO parkingLotDeviceKeTopDTO = keTopHttpService.queryDeviceStatus(vehicleIntegratedConfigBO, new QueryParkingLotDeviceKeTopDTO());
        if(parkingLotDeviceKeTopDTO != null) {
            List<ParkingLotDeviceInfoKeTopDTO> parkingLotDeviceInfoKeTopDTOList = parkingLotDeviceKeTopDTO.getDeviceList();
            List<Device> deviceList = handlerDeviceStatus(vehicleSystemInfoBO, parkingLotDeviceInfoKeTopDTOList);
            List<Device> offLineDevices = deviceList.stream().filter(s->s.getOnlineStatus().equals(DeviceOnlineStatusEnum.OFFLINE)).collect(Collectors.toList());
            // 更新设备状态
            deviceService.asyncUpdateOnlineStatus(deviceList);

            DeviceQuery deviceQuery = new DeviceQuery();
            deviceQuery.setProjectId(projectId);
            deviceQuery.setThirdDeviceIdList(deviceList.stream().map(Device::getThirdDeviceId).collect(Collectors.toList()));
            List<DeviceDTO> deviceDTOList = deviceService.list(null, deviceQuery);

            // 发送离线告警
            offLineDevices.forEach(device->{
                DeviceDTO deviceDTO = deviceDTOList.stream().filter(s-> StringUtils.equals( s.getThirdDeviceId(), device.getThirdDeviceId()))
                        .findAny().orElse(null);
                if(deviceDTO != null) {
                    vehicleAlarmKafkaService.sendAlarmForVehicleDeviceStatusOffline(projectId, deviceDTO.getId());
                }
            });
        }
        log.info("KeyTop sync device status end!");
    }

    private List<Device> handlerDeviceStatus(VehicleSystemInfoBO configDTO, List<ParkingLotDeviceInfoKeTopDTO> list) {
        List<Device> deviceList = new ArrayList<>();
        if(ChkUtil.isNotNull(list)) {
            for (ParkingLotDeviceInfoKeTopDTO item : list) {
                Device device = new Device();
                device.setProjectId(configDTO.getProjectId());
                device.setThirdDeviceId(item.getDeviceCode());
                device.setDeviceTypeId(3);
                device.setOnlineStatus(Integer.valueOf(0).equals(item.getDeviceStatus()) ? DeviceOnlineStatusEnum.OFFLINE : DeviceOnlineStatusEnum.ONLINE);
                deviceList.add(device);
            }
        }
        return deviceList;
    }

}
